import { API } from '../config/http.config'
import { UserInfo, AdminInfo, ManagerInfo } from '../config/mock.config'

export function setupAuthMock(mock) {
    mock.onPost(API.userLogin).reply(config => {
        const { username, password } = JSON.parse(config.data || '{}')

        const validUser = {
            username: 'user',
            password: '123456'
        }

        if (username === validUser.username && password === validUser.password) {
            // 登录成功，返回token等信息
            return [200, UserInfo]
        } else {
            // 登录失败
            return [401, { message: '用户名或密码错误' }]
        }
    })

    mock.onPost(API.adminLogin).reply(config => {
        const { username, password } = JSON.parse(config.data || '{}')

        const validAdmin = {
            username: 'admin',
            password: 'admin123'
        }

        const validManager = {
            username: 'manager',
            password: 'manager123'
        }

        if (username === validAdmin.username && password === validAdmin.password) {
            // 管理员登录成功
            return [200, AdminInfo]
        } else if (username === validManager.username && password === validManager.password) {
            // Manager登录成功
            return [200, ManagerInfo]
        } else {
            // 登录失败
            return [401, { message: '用户名或密码错误' }]
        }
    })
}